<?php 
//stoped using this library 
/* this file is use for all mongo implimentation of this project.
 developed by lakmal and samila.
all string have to convert to UTF-8 by using <string utf8_encode ( string $data )>
*/
$dbNamecon; // open a connection to the database
$debug=false; // debug mode but not implimented yet implement it
$dbNameName;

/* $conf['db_name'] = "vesuvius";
 $conf['db_host'] = "localhost";
$conf['db_port'] = "3306";
$conf['db_user'] = "root";
$conf['db_pass'] = ""; */

class libMongo{

	/* Opens a connection to the desired mongo DB and stores the connection in the object
	 *
	* @param $name string name of the database
	* @param $host string
	* @param $port
	* @param $user
	* @param $pass
	*external refferences http://docs.mongodb.org/manual/reference/connection-string/*/
	function MongoDbConnection($host=NULL,$port=NULL,$user=NULL,$pass=NULL){

		// connect
		return $this->$dbNamecon = new MongoClient('mongodb://'.$user.':'.$pass.'@'.$host.':'.$port);


	}

	function connect ($name){


		// select a database
		$this->$dbNameName = $m->$name;

	}

	/* insert data array into the desired collection
	 *
	* @param $collection	string	name of the collection
	* @param $array			array	array of the document
	* return $ins        	boolean	true if succeeded*/

	function insertArray($collection,Array $document=NULL){

		$collection = $dbName->$collection;

		$ins = $collection->insert($document);

		return $ins;

	}


	/***
	 *
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $column     String  Name of the Column to go for
	*  @param  $filter     Array   Filter settings to use(conditions like SELECT * FROM WHERE <CONDITION>)
	*  @param  $order      Array   Sort preferences
	*  @param  $slaveOkay  Bool    If a read from the slave is allowed / NOT IMPLEMENTED YET
	*
	*  @return result or false
	***/
	function getOne($collection, $column, Array $filter=array(), Array $order=array(), $slaveOkay=true){
		//get all burning items from the queue
		$cursor = $this->db->$collection->find($filter);  //SELECT * FROM $collection WHERE ^^^



		if($order){
			$cursor->sort($order);  //ORDER BY $order ASC
		}
		$cursor->limit(1);          //LIMIT 1

		/* $this->debug('<span style="color:#0A0;">getOne</span> from '.$collection.', column '.$column.', filter:<br />'.print_r($filter, true).'Order By:<br />'.print_r($order,true));
		 */

		foreach($cursor as $obj){
			//close cursor before return
			unset($cursor);

			return $obj[$column];
		}

		return false;
	}


	/***
	 *  Find one Document Key-Val by MongoID
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $id         String  MongoID of the Document to Delete
	*
	*  @return mixed
	*  WILL NOT BE USED IN THE PROJECT
	***/
	function getOneByID($collection, $column, $id){
		return $this->getOne($collection, $column, array('_id' => new MongoId($id)));
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter     Array   Filter settings to use
	*  @param  $order      Array   Sort preferences
	*  @param  $start      Int     LIMIT _X_, Y (if set)
	*  @param  $offset     Int     LIMIT X, _Y_ (if set)
	*
	*  @return array of arrays
	***/
	function getAll($collection, Array $filter=array(), Array $order=array(), $start=null, $offset=null){
		$cursor = $this->db->$collection->find($filter)->limit((int)$start)->skip((int)$offset);  //SELECT * FROM $collection WHERE ^^^

		if($order){
			$cursor->sort($order);  //ORDER BY $order ASC
		}

		//limit x,y
		/*
		 if($start){
		$cursor->limit($start);
		}

		if($offset){
		$cursor->skip((int)$offset);
		}
		*/

		$return = array();
		foreach($cursor as $obj){
			$return[] = $obj;
		}

		//close cursor before return
		unset($cursor);

		/* $this->debug('<span style="color:#0A0;">getAll</span> from '.$collection.' filter:<br />'.print_r($filter, true).'Order:<br/>'.print_r($order,true).'LIMIT: '.$start.' OFFSET: '.$offset);
		 */
		return $return;
	}


	/***
	 *  @param  $collection Array   Name of the Collection (Table) to query
	*  @param  $column     String  Reduce the resultset to THIS column
	*  @param  $filter     Array   Filter settings to use
	*  @param  $order      Array   Sort preferences
	*  @param  $start      Int     LIMIT _X_, Y (if set)
	*  @param  $offset     Int     LIMIT X, _Y_ (if set)
	*
	*  @return array representing a columnt from a collection
	***/
	function getColumn($collection, $column, Array $filter=array(), Array $order=array(), $start=null, $offset=null){
		$result = $this->getAll($collection, $filter, $order, $start, $offset);
		$return = array();
		foreach($result as $obj){
			$return[] = $obj[$column];
		}

		return $return;
	}


	/***
	 *  Shorthand alias for the function above getColumn()
	*  @param  $collection Array   Name of the Collection (Table) to query
	*  @param  $column     String  Reduce the resultset to THIS column
	*  @param  $filter     Array   Filter settings to use
	*  @param  $order      Array   Sort preferences
	*  @param  $start      Int     LIMIT _X_, Y (if set)
	*  @param  $offset     Int     LIMIT X, _Y_ (if set)
	*
	*  @return array representing a columnt from a collection
	***/
	function getCol($collection, $column, Array $filter=array(), Array $order=array(), $start=null, $offset=null){
		return $this->getColumn($collection, $column, $filter, $order, $start, $offset);
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter     Array   Filter settings to use
	*  @param  $order      Array   Sort preferences
	*
	*  @return array representing one document (however complex it may be)
	***/
	function getRow($collection, Array $filter=array(), Array $order=array()){
		$result = $this->getAll($collection, $filter, $order, 1);
		foreach($result as $obj){
			return $obj;
		}
		return array();
	}


	/***
	 *  Find one Document by MongoID
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $id         String  MongoID of the Document to Delete
	*
	*  @return array representing one document (however complex it may be)
	*  WILL NOT BE USED IN THE PROJECT
	***/
	function getRowByID($collection, $id){
		return $this->getRow($collection, array('_id' => new MongoId($id)));
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter     Array   Filter settings to use
	*  @param  $order      Array   Sort preferences
	*  @param  $start      Int     LIMIT _X_, Y (if set)
	*  @param  $offset     Int     LIMIT X, _Y_ (if set)
	*
	*  @return array of arrays with the keys of the first array taken from a filter
	***/
	function getAssoc($collection, $key, $val=null, Array $filter=array(), Array $order=array(), $start=null, $offset=null){
		$result = $this->getAll($collection, $filter, $order, $start, $offset);
		$return = array();
		if($val){ //it eez faster to have the IF outside of the loop
			foreach($result as $obj){
				$return[$obj[$key]] = $obj[$val];
			}
		}else{
			foreach($result as $obj){
				$return[$obj[$key]] = $obj;
			}
		}

		/* //adding debug info where neccessary
		 $this->debug('<span style="color:#0A0;">getAssoc</span> from '.$collection.' with filter:<br />'.print_r($filter,true).'<br />key is: '.$key.'<br />Order: '.print_r($order,true).'<br />Start: '.$start.'<br />Offset:'.$offset);
		*/
		return $return;
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $document   Array   The document to store in the collection
	*
	*  @return void
	***/
	function insert($collection, Array $document){
		$this->db->$collection->insert($document);

		/* 	$this->debug('<span style="color:#0A0;">Inserted</span> Document into '.$collection.':<br />'.print_r($document, true));
		 */
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter
	*  @param  $data
	*  @param  $options
	***/
	function update($collection, Array $filter, Array $data, Array $options=null){
		if(!isset($options['multiple'])){
			$options['multiple'] = true; //enabled per default
		}

		if(!isset($options['upsert'])){
			$options['upsert']   = $this->default_upsert_state; //disabled per default!!!
		}

		//cook data for the update
		$data = array('$set' => $data);

		//did I pass an incrementation string with the options?
		if(isset($options['$inc'])){
			$data['$inc'] = $options['$inc'];
			unset($options['$inc']);
		}

		/* 	//debug message
		 $this->debug('<span style="color:#AA0;">Updated</span> Document in '.$collection.':<br />'.print_r($data, true).'with filter:<br />'.print_r($filter, true).'Using these options:<br />'.print_r($options, true));
		*/
		return $this->db->$collection->update($filter, $data, $options);
	}


	/***
	 *  Same as update with will alter maximum of one record
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter
	*  @param  $data
	*  @param  $options
	***/
	function updateOne($collection, Array $filter, Array $data, Array $options=null){
		//update one record only
		$options['multiple'] = false;
		return $this->update($collection, $filter, $data, $options);
	}


	/***
	 *  Same as update, updates only one records found by ID
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $id
	*  @param  $data
	*  @param  $options
	***/
	function updateOneByID($collection, $id, Array $data, Array $options=null){
		//update one record only
		$options['multiple'] = false;

		if (!($id instanceof MongoId)) {
			$id = new MongoId($id);
		}

		return $this->update($collection, array('_id' => $id), $data, $options);
	}


	/***
	 *  Shorthand alias for the function above
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $id
	*  @param  $data
	*  @param  $options
	***/
	function updateByID($collection, $id, Array $data, Array $options=null){
		return $this->updateOneByID($collection, $id, $data, $options);
	}


	/***
	 *  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter     Array   Filter settings to use
	*
	*  @return boolean
	***/
	function delete($collection, Array $filter=array()){
		/*
		 $this->debug('<span style="color:#F00;">Delete</span> from '.$collection.' filter:<br />'.print_r($filter, true));
		*/
		return $this->db->$collection->remove($filter);
	}


	/***
	 *  Remove a document from Collection based on its MongoDB
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $id         String  MongoID of the Document to Delete
	*
	*  @return boolean
	***/
	function deleteByID($collection, $id){
		if (!($id instanceof MongoId)) {
			$id = new MongoId($id);
		}

		return $this->delete($collection, array('_id' => $id), true);
	}


	/***
	 *  Count the number of matching documents
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $filter     Array   Filter settings to use
	*  @param  $start      Int     LIMIT _X_, Y (if set)
	*  @param  $offset     Int     LIMIT X, _Y_ (if set)
	*
	*  @return integer
	***/
	function count($collection, Array $filter=array(), $start=null, $offset=null){
		if(count($filter)>0){
			return $this->db->$collection->count($filter, $start, $offset);
		}else{
			return $this->db->$collection->count();
		}
	}


	/***
	 *  Aggregation in MongoDB is fun
	*
	*  @param  $collection String
	*  @param  $keys
	*  @param  $initial
	*  @param  $reduce
	***/
	function group($collection, Array $keys, Array $initial, $reduce, Array $filter=array()){
		return $this->db->$collection->group($keys, $initial, $reduce, $filter);
	}


	function groupAssoc($collection, Array $keys, Array $initial, $reduce, Array $filter=array()){
		$data = $this->group($collection, $keys, $initial, $reduce, $filter);
		$key  = array_shift(array_keys($keys));
		$val  = array_shift(array_keys($initial));

		$return = array();
		foreach($data['retval'] as $item){
			$return[$item[$key]] = $item[$val];
		}
		return $return;
	}


	/***
	 *  Executes some other Mongo Command vie the "command" method internal to mongodb
	*  this method is named "Execute" just to resemble the common "Execute" method
	*  in AdoDB, it is, however, not a pipe to mongo db's execute method
	*
	*  @param  $command    Array   Full Mongo command to Execute
	*
	*  @return mixed the command return values
	***/
	function Execute(Array $command){

		/* $this->debug('Executing Command: '.print_r($command, true));
		 */
		$cursor = $this->db->command($command);
		$return = array();
		foreach($cursor as $obj){
			$return[] = $obj;
		}

		//close cursor before return
		unset($cursor);

		/* $this->debug('Admin Command Result: '.print_r($return, true));
		 */
		return $return;
	}


	/***
	 *  Executes a command agains the Admin Database (useful for RS operations)
	*
	*  @param  $command    Array   Full Mongo command to Execute
	*
	*  @return mixed the command return values
	***/
	function ExecuteAdminCommand(Array $command){
		$this->setDBName('admin');
		$return = $this->Execute($command);
		$this->setDBName($this->dbName); //back to the original DB
		return $return;
	}


	/***
	 *  Convenience method for creating an index
	*
	*  @param  $collection String  Name of the Collection (Table) to query
	*  @param  $settings   Array   Indexing Settings
	***/
	function ensureIndex($collection, Array $settings, Array $options=array()){
		return $this->db->$collection->ensureIndex($settings, $options);
	}


	/***
	 *  Used for outputting debug info to browser
	***/
	/* 	function debug($message){
		if($this->debug){
	echo '<blockquote style="padding: 10px; margin: 5px; background: #eee; border: 1px solid #ccc;">
	<pre>'.date('Y-m-d H:i:s').' '.trim($message).'</pre>
	</blockquote>';
	}
	} */
}
?>